package view.common;

import javax.swing.*;
import javax.swing.table.TableCellEditor;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ButtonEditor extends AbstractCellEditor implements TableCellEditor {
    private JPanel panel;
    private JButton editButton;
    private JButton deleteButton;
    private String value;
    private ActionListener actionListener;
    
    public ButtonEditor(ActionListener listener) {
        this.actionListener = listener;
        panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 0));
        
        editButton = new JButton("编辑");
        deleteButton = new JButton("删除");
        
        // 设置按钮的首选大小
        Dimension buttonSize = new Dimension(60, 25);
        editButton.setPreferredSize(buttonSize);
        deleteButton.setPreferredSize(buttonSize);
        
        editButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        deleteButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        
        // 设置面板不透明
        panel.setOpaque(true);
        
        editButton.addActionListener(e -> {
            actionListener.actionPerformed(new ActionEvent(
                editButton, ActionEvent.ACTION_PERFORMED, "编辑"));
            fireEditingStopped();
        });
        
        deleteButton.addActionListener(e -> {
            actionListener.actionPerformed(new ActionEvent(
                deleteButton, ActionEvent.ACTION_PERFORMED, "删除"));
            fireEditingStopped();
        });
        
        panel.add(editButton);
        panel.add(deleteButton);
    }
    
    @Override
    public Component getTableCellEditorComponent(JTable table, Object value,
            boolean isSelected, int row, int column) {
        this.value = (value == null) ? "" : value.toString();
        panel.setBackground(table.getSelectionBackground());
        return panel;
    }
    
    @Override
    public Object getCellEditorValue() {
        return value;
    }
} 